import json

# TODO  美国
f_us = open("E:/excel学习文档-表格/黑马-可视化案例数据/折线图-美国.txt","r",encoding = "UTF-8")
us_data = f_us.read()

# 去掉不合Json规范的开头 .....  jsonp_1629344292311_69436( ..... 替换成空字符
us_data = us_data.replace("jsonp_1629344292311_69436(","")

# 去掉不合Json规范的结尾 .....   );
# us_data = us_data[:-2]
us_data = us_data.replace(");","")  # 或者： Us_data[:-2]

# Json转Python字典
us_dict = json.loads(us_data)
# print(type(us_dict))
# print(us_dict)

# 获取trend key
us_trend_data = us_dict['data'][0]['trend']
# print(type(trend_data))
# print(trend_data)

# 获取日期数据，用于x轴，取2020年（到314下标结束）
us_x_data = us_trend_data['updateDate'][:314]
# print(x_data)

# 获取确诊数据，用于y轴，取2020年（到314下标结束）
us_y_data = us_trend_data['list'][0]['data'][:314]
# print(us_y_data)

# TODO  印度
f_in =open("E:/excel学习文档-表格/黑马-可视化案例数据/折线图-印度.txt",'r',encoding = "UTF-8")
in_data = f_in.read()
in_data = in_data.replace("jsonp_1629350745930_63180(","")
in_data = in_data.replace(");","")
in_dict = json.loads(in_data)
in_trend_data = in_dict['data'][0]['trend']
in_x_data = in_trend_data['updateDate'][:314]
in_y_data = in_trend_data['list'][0]['data'][:314]
# print(in_y_data)

# TODO 日本
f_jp = open("E:/excel学习文档-表格/黑马-可视化案例数据/折线图-日本.txt",'r',encoding = "UTF-8")
jp_data = f_jp.read()
jp_data= jp_data.replace("jsonp_1629350871167_29498(","")
jp_data = jp_data.replace(");","")
jp_data = json.loads(jp_data)
jp_trend_data = jp_data['data'][0]['trend']
jp_x_data =jp_trend_data['updateDate'][:314]
jp_y_data = jp_trend_data['list'][0]['data']
# print(jp_y_data)


# TODO 生成图表
from pyecharts.charts import Line
from pyecharts.options import TitleOpts,LegendOpts,ToolboxOpts,VisualMapOpts,LabelOpts

line = Line()
line.add_xaxis(us_x_data) # x轴共用
line.add_yaxis("美国确诊人数",us_y_data,label_opts = LabelOpts(is_show = False))
line.add_yaxis("日本确诊人数",jp_y_data,label_opts = LabelOpts(is_show = False))
line.add_yaxis("印度确诊人数",in_y_data,label_opts = LabelOpts(is_show = False))
line.set_global_opts(
    title_opts = TitleOpts(title = "2020年美日印三国新冠确诊人数对比折线图",pos_left="center",pos_bottom ="1%"),
    # legend_opts = LegendOpts(is_show = True),
    # toolbox_opts = ToolboxOpts(is_show = True),
    # visualmap_opts = VisualMapOpts(is_show =True)

)

line.render("美日印三国确诊人数.html")

f_us.close()
f_jp.close()
f_in.close()